package com.tmt.annotation.parser;

import com.tmt.annotation.ScopeDescriptor;
import com.tmt.helper.ReflectHelper;

import java.lang.reflect.Field;

/**
 * 该类用于读取字段上的ScopeDescriptor注解的属性值。
 * Created by ThreeManTeam on 201711/14.
 */
public class ScopeDescriptorParser extends DescriptorParser<ScopeDescriptor>  {

    public ScopeDescriptorParser(Class<ScopeDescriptor> annotationClazz) {
        super(annotationClazz);
    }


    /**
     * 获取clazz类上的fieldName字段的ScopeDescriptor注解的value属性值。
     * @param clazz
     * @param fieldName
     * @param defaultValue
     * @return
     */
    public String getValue(Class<?> clazz, String fieldName, String defaultValue){
        Field target = ReflectHelper.findFieldByName(clazz, fieldName);
        String title = _getTitle(target);
        return title != null ? title : defaultValue;
    }


    /***************************************************************************************************/
    /**********************私有方法*********************************************************************/

    private String _getTitle(Field target){
        if (target != null) {
            ScopeDescriptor[] array = target.getAnnotationsByType(ScopeDescriptor.class);
            if (array.length > 0)
                return array[0].value();
        }
        return null;
    }

}
